package com.hsq.androidproject.ui.recommend_fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyGridViewStarDTAdapter;
import com.hsq.androidproject.adapter.Recommend.HanjuAdapter;
import com.hsq.androidproject.entity.StarDynamic;
import com.hsq.androidproject.entity.StarDynamicTmp;
import com.hsq.androidproject.entity.StarTujian;
import com.hsq.androidproject.entity.TJEntity;
import com.hsq.androidproject.entity.find.FindVideos;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.List;

import static com.example.lenovo.androidproject3.R.id.mGridViewdt05;

/**
 * A simple {@link Fragment} subclass.
 */
public class RRecommendFragment extends Fragment implements View.OnClickListener {

    private GridView mGridViewCen, mGridViewdt, mGridViewdt03, mGridViewdt04, wmGridViewdt05, mGridViewdt06, wmGridViewdt07, mGridViewdt08;
    private TextView tv_lefttoptitle;
    private FragmentManager manager;
    private android.support.v4.app.FragmentTransaction transaction;
    private StarTujian entity;
    private List<StarTujian> list = new ArrayList<>();

    public RRecommendFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_rrecommend, container, false);
        inint(view);
        inintData(view);

        return view;
    }

    private void inintData(View view) {
        //热门韩剧
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/index/recommend", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                TJEntity tjEntity = new Gson().fromJson(response, TJEntity.class);
                List<TJEntity.RecsBean.ItemsBean> playItemsBean = tjEntity.getRecs().get(0).getItems();
                HanjuAdapter adapter = new HanjuAdapter(getContext(), playItemsBean);
                mGridViewCen.setAdapter(adapter);
            }
        });
        //明星动态
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/star/hotVideos?offset=0&count=30", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                StarDynamic starDynamic = new Gson().fromJson(response, StarDynamic.class);
                List<StarDynamicTmp> tmps = new ArrayList<>();
                for (int i = 0; i <= 5; i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(starDynamic.getVideos().get(i).getThumb());
                    tmp.setVid(starDynamic.getVideos().get(i).getVid());
                    tmp.setTitle(starDynamic.getVideos().get(i).getTitle());
                    for (int j = 0; j < starDynamic.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(starDynamic.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(starDynamic.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0, 32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getActivity());
                mGridViewdt.setAdapter(adapter);
            }
        });
        //美妆护肤
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/cate/videos?cid=3&offset=0&count=20", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                FindVideos findVideos = new Gson().fromJson(response, FindVideos.class);
                ArrayList<StarDynamicTmp> tmps = new ArrayList<>();
                for (int i = 0; i <= 5; i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(findVideos.getVideos().get(i).getThumb());
                    tmp.setVid(findVideos.getVideos().get(i).getVid());
                    tmp.setTitle(findVideos.getVideos().get(i).getTitle());
                    for (int j = 0; j < findVideos.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(findVideos.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(findVideos.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0, 32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getContext());
                mGridViewdt03.setAdapter(adapter);
            }
        });
        //娱乐八卦
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/cate/videos?cid=10&offset=0&count=20", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                FindVideos findVideos = new Gson().fromJson(response, FindVideos.class);
                ArrayList<StarDynamicTmp> tmps = new ArrayList<>();
                for (int i = 0; i <= 5; i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(findVideos.getVideos().get(i).getThumb());
                    tmp.setVid(findVideos.getVideos().get(i).getVid());
                    tmp.setTitle(findVideos.getVideos().get(i).getTitle());
                    for (int j = 0; j < findVideos.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(findVideos.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(findVideos.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0, 32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getContext());
                mGridViewdt04.setAdapter(adapter);
            }
        });
        //美食精选
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/cate/videos?cid=1&offset=0&count=20", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                FindVideos findVideos = new Gson().fromJson(response, FindVideos.class);
                ArrayList<StarDynamicTmp> tmps = new ArrayList<>();
                for (int i = 0; i <= 5; i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(findVideos.getVideos().get(i).getThumb());
                    tmp.setVid(findVideos.getVideos().get(i).getVid());
                    tmp.setTitle(findVideos.getVideos().get(i).getTitle());
                    for (int j = 0; j < findVideos.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(findVideos.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(findVideos.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0, 32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getContext());
                wmGridViewdt05.setAdapter(adapter);
            }
        });
        //星座运势
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/cate/videos?cid=5&offset=0&count=20", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                FindVideos findVideos = new Gson().fromJson(response, FindVideos.class);
                ArrayList<StarDynamicTmp> tmps = new ArrayList<>();
                for (int i = 0; i <= 5; i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(findVideos.getVideos().get(i).getThumb());
                    tmp.setVid(findVideos.getVideos().get(i).getVid());
                    tmp.setTitle(findVideos.getVideos().get(i).getTitle());
                    for (int j = 0; j < findVideos.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(findVideos.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(findVideos.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0, 32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getContext());
                mGridViewdt06.setAdapter(adapter);
            }
        });
        //减肥健康
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/cate/videos?cid=4&offset=0&count=20", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                FindVideos findVideos = new Gson().fromJson(response, FindVideos.class);
                ArrayList<StarDynamicTmp> tmps = new ArrayList<>();
                for (int i = 0; i <= 5; i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(findVideos.getVideos().get(i).getThumb());
                    tmp.setVid(findVideos.getVideos().get(i).getVid());
                    tmp.setTitle(findVideos.getVideos().get(i).getTitle());
                    for (int j = 0; j < findVideos.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(findVideos.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(findVideos.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0, 32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getContext());
                wmGridViewdt07.setAdapter(adapter);
            }
        });
        //旅游推荐
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/cate/videos?cid=2&offset=0&count=20", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                FindVideos findVideos = new Gson().fromJson(response, FindVideos.class);
                ArrayList<StarDynamicTmp> tmps = new ArrayList<>();
                for (int i = 0; i <= 5; i++) {
                    StarDynamicTmp tmp = new StarDynamicTmp();
                    tmp.setThumb(findVideos.getVideos().get(i).getThumb());
                    tmp.setVid(findVideos.getVideos().get(i).getVid());
                    tmp.setTitle(findVideos.getVideos().get(i).getTitle());
                    for (int j = 0; j < findVideos.getVideos().get(i).getSources().size(); j++) {
                        tmp.setOffset(findVideos.getVideos().get(i).getSources().get(j).getOffset());
                        tmp.setPage(findVideos.getVideos().get(i).getSources().get(j).getPage());
                    }
                    String substring = tmp.getPage().substring(0, 32);
                    if (substring.equals("http://www.bilibili.com/video/av")) {
                        tmps.add(tmp);
                    }
                }
                MyGridViewStarDTAdapter adapter = new MyGridViewStarDTAdapter(tmps, getContext());
                mGridViewdt08.setAdapter(adapter);
            }
        });

    }

    public void inint(View view) {
        manager = getFragmentManager();
        mGridViewCen = (GridView) view.findViewById(R.id.mGridViewCen);
        tv_lefttoptitle = (TextView) view.findViewById(R.id.tv_title);
        mGridViewdt = (GridView) view.findViewById(R.id.mGridViewdt);
        mGridViewdt03 = (GridView) view.findViewById(R.id.mGridViewdt03);
        mGridViewdt04 = (GridView) view.findViewById(R.id.mGridViewdt04);
        wmGridViewdt05 = (GridView) view.findViewById(mGridViewdt05);
        mGridViewdt06 = (GridView) view.findViewById(R.id.mGridViewdt06);
        wmGridViewdt07 = (GridView) view.findViewById(R.id.mGridViewdt07);
        mGridViewdt08 = (GridView) view.findViewById(R.id.mGridViewdt08);

        mGridViewCen.setFocusable(false);
        mGridViewdt.setFocusable(false);
        mGridViewdt03.setFocusable(false);
        mGridViewdt04.setFocusable(false);
        wmGridViewdt05.setFocusable(false);
        mGridViewdt06.setFocusable(false);
        wmGridViewdt07.setFocusable(false);
        mGridViewdt08.setFocusable(false);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.relatdt01:
                RTvFragment rTvFragment = new RTvFragment();
                transaction = manager.beginTransaction();
                transaction.replace(R.id.scrollViews, rTvFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                break;
            case R.id.relatdt02:
                break;
            case R.id.relatdt03:
                break;
            case R.id.relatdt04:
                break;
            case R.id.relatdt05:
                break;
            case R.id.relatdt06:
                break;
            case R.id.relatdt07:
                break;
            case R.id.relatdt08:
                break;
        }
    }
}
